package com.yx.frame.builder.validate;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ServiceBuilderConfigManager {

	static ServiceBuilderConfigManager instance = null;
	String userDefineValidatorsFile = null;
	HashMap<String, Validator> allValidators = new HashMap<String, Validator>();
	List<Validator> systemValidators = new ArrayList<Validator>();
	List<Validator> userDefineValidators = new ArrayList<Validator>();
	String[] dataSource;
	String[] dataSourceValues;
	String serverUrl="http://localhost:8080/";
	int serverPort=8080;
	int minThreadPoolSize=20;
	int maxThreadPoolSize=100;
	int maxAsyncThreadPoolSize=100;
	int minAsyncThreadPoolSize=20;
	int maxUploadFileSzie=20;
	
	String externalController="";

	private ServiceBuilderConfigManager() {

	}

	public static ServiceBuilderConfigManager getInstance() {
		if (instance == null) {
			instance = new ServiceBuilderConfigManager();
		}
		return instance;
	}
	public void refrashValidators() {
		if(userDefineValidatorsFile==null)return;
		refrashValidators(userDefineValidatorsFile);
	}

	public void refrashValidators(String fileName) {

		allValidators = new HashMap<String, Validator>();
		systemValidators = new ArrayList<Validator>();
		userDefineValidators = new ArrayList<Validator>();
		SystemValidators.init();		
		this.userDefineValidatorsFile = fileName;
		if(userDefineValidatorsFile==null)return;
		File file = new File(userDefineValidatorsFile);
		if (!file.exists()) {
			return;
		}
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(file);
			Element root = document.getRootElement();
			if ("ServiceBuilderConfig".equals(root.getName())) {
				serverUrl=root.getChild("ServerUrl").getAttributeValue("value");
				try{
					serverPort=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("port"));
				}catch(Exception e){}
				try{
					minThreadPoolSize=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("MinThreadPoolSize"));
				}catch(Exception e){}
				try{
					maxThreadPoolSize=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("MaxThreadPoolSize"));
				}catch(Exception e){}
				try{
					minAsyncThreadPoolSize=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("MinAsyncThreadPoolSize"));
				}catch(Exception e){}
				try{
					maxAsyncThreadPoolSize=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("MaxAsyncThreadPoolSize"));
				}catch(Exception e){}
				try{
					maxUploadFileSzie=Integer.parseInt(root.getChild("ServerUrl").getAttributeValue("MaxUploadFileSzie"));
				}catch(Exception e){}
				externalController=root.getChild("ServerUrl").getAttributeValue("ExternalController");
				//dataSourceStr=root.getChild("ServiceDataSource").getAttributeValue("value");
				//dataSource=dataSourceStr.split(",");
				List<Element> dsList = root.getChild("ServiceDataSource").getChildren();
				dataSource=new String[dsList.size()];
				dataSourceValues=new String[dsList.size()];
				for (int i = 0; i < dsList.size(); i++) {
					Element dsElement = dsList.get(i);
					String name = dsElement.getAttributeValue("name");
					dataSource[i]=name;
					dataSourceValues[i]=dsElement.getAttributeValue("value");					
				}
				
				List<Element> validatorList = root.getChild("ServiceBuilderValidator").getChildren();
				for (int i = 0; i < validatorList.size(); i++) {
					Element validatorElement = validatorList.get(i);
					String name = validatorElement.getAttributeValue("name");
					String staticMethodName = validatorElement
							.getAttributeValue("staticMethodName");
					String comment = validatorElement
							.getAttributeValue("comment");
					Validator validator = new Validator(name,
							staticMethodName, comment);
					addUserDefineValidator(validator);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	public int getServerPort() {
		return serverPort;
	}

	public String getExternalController() {
		return externalController;
	}

	public String[] getDataSource() {
		return dataSource;
	}
	public String[] getDataSourceValues() {
		return dataSourceValues;
	}

	public String getServerUrl() {
		return serverUrl;
	}

	public Validator getValidator(String name) {
		return allValidators.get(name);
	}

	public void addSystemValidator(Validator validator) {
		systemValidators.add(validator);
		allValidators.put(validator.getName(), validator);
	}

	public void addUserDefineValidator(Validator validator) {
		userDefineValidators.add(validator);
		allValidators.put(validator.getName(), validator);
	}

	public void removeUserDefineValidator(int idx) {
		Validator validator = userDefineValidators.get(idx);
		userDefineValidators.remove(idx);
		allValidators.remove(validator.getName());
	}

	public List<Validator> getSystemValidators() {		
		return systemValidators;
	}
	
	public String getUserDefineValidatorsFile() {
		return userDefineValidatorsFile;
	}

	public List<Validator> getUserDefineValidators(String type) {
		return userDefineValidators;
	}
	
	public List<Validator> getUserDefineValidators() {
		return userDefineValidators;
	}	
	public int getMinThreadPoolSize() {
		return minThreadPoolSize;
	}

	public int getMaxThreadPoolSize() {
		return maxThreadPoolSize;
	}

	public int getMaxAsyncThreadPoolSize() {
		return maxAsyncThreadPoolSize;
	}

	public int getMinAsyncThreadPoolSize() {
		return minAsyncThreadPoolSize;
	}


	public int getMaxUploadFileSzie() {
		return maxUploadFileSzie;
	}

}
